﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bootstrap;
using DevExpress.XtraEditors;
using Dogshow.Win.DataAccess;
using Dogshow.Win.Models;
using Ninject;
using System.Linq;
using Kinolog = Dogshow.Model.Kinolog;

namespace Dogshow.Win.Views
{
    public partial class PretragaPasa : DevExpress.XtraEditors.XtraForm
    {
        public KriterijumPretragePasa KriterijumPretragePasa { get; set; }

        public PretragaPasa()
        {
            InitializeComponent();
            simpleButton1.Click += (s, e) => OnPretrazi();
            simpleButton2.Click += (s, e) => OnOtkazi();
            gridView1.DoubleClick += (s, e) => OnSelected();
            gridView1.CustomColumnDisplayText += (s, e) =>
            {
                if (e.Column.FieldName == "Vlasnik" || e.Column.FieldName == "Odgajivac")
                {
                    var val = (Kinolog)e.Value;
                    e.DisplayText = val != null ? val.ImeIPrezime : null;
                }
            };
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                OnPretrazi();
            } else if (e.KeyCode == Keys.Escape)
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
        }

        public Pas IzabranPas { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            fciGrupaBindingSource.DataSource = GetDataService().VratiFciGrupe().ToArray();
            rasaPsaBindingSource.DataSource = GetDataService().VratiRasePasa().ToArray();
            KriterijumPretragePasa = new KriterijumPretragePasa();
            kriterijumPretragePasaBindingSource.DataSource = KriterijumPretragePasa;
        }

        private void OnPretrazi()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                pasBindingSource.DataSource = GetDataService().PretraziPse(KriterijumPretragePasa).Select(ModelMapper.Map);
            }
            catch (Exception exc)
            {
                XtraMessageBox.Show(this, "Greška prilikom pretrage. Pokušajte ponovo.", "Upozorenje", MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);
            } finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void OnSelected()
        {
            IzabranPas = gridView1.GetFocusedRow() as Pas;
            DialogResult = DialogResult.OK;
            Close();
        }

        private void OnOtkazi()
        {
            KriterijumPretragePasa = new KriterijumPretragePasa();
            kriterijumPretragePasaBindingSource.DataSource = KriterijumPretragePasa;
            pasBindingSource.DataSource = typeof (Pas);
        }

        private static IDataService GetDataService()
        {
            var kernel = (IKernel)Bootstrapper.Container;
            return kernel.Get<IDataService>();
        }
    }
}